package com.common.util.Excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.List;

public class ImportListener<T> extends AnalysisEventListener<T> {
    private int batchCount = 3000;
    private List<T> list = new ArrayList();
    private final ExcelImporter<T> importer;

    public void invoke(T data, AnalysisContext analysisContext) {
        this.list.add(data);
        if (this.list.size() >= this.batchCount) {
            this.importer.save(this.list);
            this.list.clear();
        }

    }

    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        this.importer.save(this.list);
        this.list.clear();
    }

    public int getBatchCount() {
        return this.batchCount;
    }

    public List<T> getList() {
        return this.list;
    }

    public ExcelImporter<T> getImporter() {
        return this.importer;
    }

    public void setBatchCount(final int batchCount) {
        this.batchCount = batchCount;
    }

    public void setList(final List<T> list) {
        this.list = list;
    }

    public String toString() {
        return "ImportListener(batchCount=" + this.getBatchCount() + ", list=" + this.getList() + ", importer=" + this.getImporter() + ")";
    }

    public ImportListener(final ExcelImporter<T> importer) {
        this.importer = importer;
    }


}
